/**
 * Copyright (C)  2011  chen wei hua.
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 2.0
 * or any later version published by the Free Software Foundation;
 * with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU Free Documentation License".
 */
package com.javaeye.delivery.service;

import java.util.List;

import com.javaeye.common.util.DateUtils;
import com.javaeye.delivery.dao.OrderDetailBatchInfoDAO;
import com.javaeye.delivery.dto.CustomerOrder;
import com.javaeye.delivery.dto.OrderDetailBatchInfo;

public class PrepareOutStroeService extends OrderService {
	
	private OrderDetailBatchInfoDAO orderDetailBatchInfoDao;

	public void setOrderDetailBatchInfoDao(
			OrderDetailBatchInfoDAO orderDetailBatchInfoDao) {
		this.orderDetailBatchInfoDao = orderDetailBatchInfoDao;
	}

	public void saveOrder(CustomerOrder order, List<OrderDetailBatchInfo> batchInfos) {
		order.setUpdateDate(DateUtils.now());
		order.setStatus(CustomerOrder.ORDER_STATES_PREPARE_OUT);
		getDao().saveOrder(order);
		
		
		for (OrderDetailBatchInfo batchInfo : batchInfos) {
			orderDetailBatchInfoDao.save(batchInfo);
		}
	}
	/*
	private void updatePurchaseOrder(String purchaseOrderId, Set<OrderDetail> details) {
		for (OrderDetail detail : details) {
			int productId = detail.getProductId();
			//更新订单表里的已经入库数量
			PurchaseOrderDetail purchaseOrderdetail = getPurchaseOrderDetailDao()
				.getPurchaseOrderDetail(purchaseOrderId, productId);
			purchaseOrderdetail.setInStoreNumber(purchaseOrderdetail.getInStoreNumber() + detail.getInStoreNumber());
			getPurchaseOrderDetailDao().savePurchaseOrderDetail(purchaseOrderdetail);
		}
		
		//如果全部入库完成更新订单表的状态
		PurchaseOrder purchaseOrder = getPurchaseOrderDao().getPurchaseOrderInfo(purchaseOrderId);
		if (isInstoreFinished(purchaseOrder)) {
			purchaseOrder.setStatus(IOrderService.ORDER_STATES_IN_STORE);
			getPurchaseOrderDao().savePurchaseOrder(purchaseOrder);
		}
	}
*/
	/**
	 * 是否全部入库完成
	 * @param details
	 * @return
	 */
/*	private boolean isInstoreFinished(PurchaseOrder purchaseOrder) {
		Set<PurchaseOrderDetail> alldetails = purchaseOrder.getOrderDetails();
		for (PurchaseOrderDetail detail : alldetails) {
			if (detail.getInStoreNumber() < detail.getNumber()) {
				return false;
			}
		}
		return true;
	}
	*/
}
